package com.cy.study.controller;

import com.cy.study.common.Response;
import com.cy.study.entity.MajorEntity;
import com.cy.study.req.UserLoginReq;
import com.cy.study.req.UserRegisterReq;
import com.cy.study.service.MajorService;
import com.cy.study.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用户登录注册控制器
 */
@RequiredArgsConstructor
@RequestMapping("/user")
@RestController
@CrossOrigin
@Slf4j
public class UserController {

    private final UserInfoService userInfoService;




    /**
     * 用户注册返回id
     * @param req
     * @return
     */
    @PostMapping("/register")
    public Response<Integer> userRegister(@RequestBody UserRegisterReq req){
        Integer id = userInfoService.userRegister(req);
        return Response.success(id);
    }

    /**
     * 更新用户头像
     * @param file
     * @param userId
     * @return
     */
    @PostMapping("/uploadPic")
    public Response uploadPic(@RequestParam("file") MultipartFile file,
                              @RequestParam("userId") Integer userId){
        userInfoService.uploadPic(file,userId,false);
        return Response.success();
    }


    /**
     * 用户登录
     * @param req
     * @return
     */
    @PostMapping("/login")
    public Response<String> userLogin(@RequestBody UserLoginReq req){
        String token = userInfoService.userLogin(req);
        return Response.success(token);
    }

}
